package cn.cvs.controller;

import cn.cvs.pojo.Supplier;
import cn.cvs.service.SupplierService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "supplier")
public class SupplierController {
    private Logger logger = Logger.getLogger(SupplierController.class);
    @Autowired
    @Qualifier("supplierServiceImpl")
    private SupplierService supplierService;

    @RequestMapping("supList")
    public String supList(Model model,@RequestParam(defaultValue = "1") Integer pageIndex,String supName, String supCode){
        Integer pageSize=5;
        //总记录数（表）
        int totalCount = supplierService.count(supName);
        int totalPageCount = totalCount/pageSize;

        if (totalCount%pageSize>0){
            totalPageCount++;
        }
        if (pageIndex>totalPageCount){
            pageIndex=totalPageCount;
        }

        List<Supplier> supplierList = supplierService.supList(pageIndex,pageSize,supName,supCode);
        model.addAttribute("supplierList",supplierList);
        model.addAttribute("supName",supName);
        model.addAttribute("supCode",supCode);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("currentPageNo",pageIndex);
        return "supplier/list";
    }
}
